package com.wlzg.collectiontool.util


import android.util.Log
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.haoge.easyandroid.easy.EasySharedPreferences
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.LoginRes
import com.wlzg.collectiontool.model.bean.LoginresSt
import com.wlzg.collectiontool.model.bean.PsHunjie
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

object MConfigUtil {
    public var logInRes: LoginRes? = null
    public var sysAllDictItems: LoginRes.SysAllDictItems?=null
    var provinceList:ArrayList<LoginRes.SysAllDictItems.OlFormBizType> = ArrayList()
    var cityList:ArrayList<ArrayList<LoginRes.SysAllDictItems.OlFormBizType>> = ArrayList()
    var townList:ArrayList<ArrayList<ArrayList<LoginRes.SysAllDictItems.OlFormBizType>>> =ArrayList()

    fun onsave(logInRess: LoginRes){
        logInRes=logInRess
        sysAllDictItems=logInRess.sysAllDictItems
        val logre=LoginresSt()
        logre.mJson= Gson().toJson(logInRess)
        GlobalScope.launch {
            withContext(Dispatchers.IO){
                AppDatabase.getInstance().userDao().insertLoginRes(logre)
            }
        }
    }
    fun initLogindata(){
        if(logInRes==null){
        GlobalScope.launch {
            withContext(Dispatchers.IO){
                if (logInRes==null){
               val logrss= AppDatabase.getInstance().userDao().getLoginres()
                val type =
                    object : TypeToken<LoginRes>() {}.type
               logInRes=  Gson().fromJson(logrss?.mJson, type)
                sysAllDictItems= logInRes?.sysAllDictItems
                }
                initQuyuData()
            }
        }}else{
            GlobalScope.launch {
                withContext(Dispatchers.IO){
                    initQuyuData()
                }}
        }
    }

    fun initQuyuData(){
        sysAllDictItems?.RYX001?.let {
            provinceList.addAll(it) }
        val list002 :ArrayList<LoginRes.SysAllDictItems.OlFormBizType>? = sysAllDictItems?.RYX002 as ArrayList<LoginRes.SysAllDictItems.OlFormBizType>?
        val list003 :ArrayList<LoginRes.SysAllDictItems.OlFormBizType>? = sysAllDictItems?.RYX003 as ArrayList<LoginRes.SysAllDictItems.OlFormBizType>?
        for(it in provinceList){
            val list :ArrayList<LoginRes.SysAllDictItems.OlFormBizType> =ArrayList()
            val areaList :ArrayList<ArrayList<LoginRes.SysAllDictItems.OlFormBizType>>  = ArrayList()

            if (list002 != null) {
                for (its in list002){
                    if(its.description.equals(it.value)) {
                        list.add(its)
                        val list1: ArrayList<LoginRes.SysAllDictItems.OlFormBizType> = ArrayList()
                        if (list003 != null) {
                            for (itss in list003) {
                                if (itss.description.equals(its.value)) {
                                    list1.add(itss)
                                }
                            }
                            areaList?.add(list1)
                        }
                    }
                }
//                if(list.size<=0){
//                    list.add(it)
//                    areaList.add(ArrayList())
//                }
                cityList?.add(list)
                townList.add(areaList)

            }
        }
    }



}